package com.example.teacher.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.ResultData;
import com.example.commonutils.ordervo.CourseForOrder;
import com.example.servicebase.client.OrderClient;
import com.example.teacher.entity.EduCourse;
import com.example.teacher.entity.chapter.ChapterVo;
import com.example.teacher.entity.chapter.VideoVo;
import com.example.teacher.entity.frontvo.CourseFrontVo;
import com.example.teacher.entity.frontvo.CourseWebVo;
import com.example.teacher.service.EduChapterService;
import com.example.teacher.service.EduCourseService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backstage/coursefront")
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //根据课程id查询课程信息
    @GetMapping(value = "getCourseForOrder/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CourseForOrder getCourseForOrder(@PathVariable String courseId) {
        CourseWebVo course = courseService.getCourseBaseInfo(courseId);
        CourseForOrder courseForOrder = new CourseForOrder();
        BeanUtils.copyProperties(course, courseForOrder);
        return courseForOrder;
    }

    //条件查询带分页查询课程
    @PostMapping(value = "getFrontCourseList/{page}/{limit}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getFrontCourseList(
            @PathVariable long page,
            @PathVariable long limit,
            @RequestBody(required = false) CourseFrontVo courseFrontVo
    ) {
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String, Object> map = courseService.getFrontCourseList(pageCourse, courseFrontVo);
        return ResultData.ok().data(map);
    }

    //获取课程详情
//    @Cacheable(value = "CourseInfo",key = "'courseId:'+#courseId")
    @GetMapping(value = "getCourseBaseInfo/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getCourseBaseInfo(@PathVariable String courseId,HttpServletRequest request) {
//        String token = request.getHeader("token");
        //获取用户id
        String token = JwtUtils.getMemberIdByJwtToken(request);
        //根据课程id获取课程的基本信息
        CourseWebVo courseWebVo = courseService.getCourseBaseInfo(courseId);
        //根据课程id得到课程的大纲
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideo(courseId);
        String videoSourceId = "";
//        if (!StringUtil.isNullOrEmpty(chapterVideoList.get(0).getChildren().get(0).getVideoSourceId())){
//            videoSourceId = chapterVideoList.get(0).getChildren().get(0).getVideoSourceId();
//        }
        if (chapterVideoList.size() != 0) {
            ChapterVo chapterVo = chapterVideoList.get(0);
            if (chapterVo != null) {
                List<VideoVo> children = chapterVo.getChildren();
                if (children.size() != 0) {
                    VideoVo videoVo = children.get(0);
                    if (videoVo != null) {
                        videoSourceId = videoVo.getId();
                    }
                }
            }
        }
        //根据课程id和用户id查询订单，根据状态判断当前用户是否成功购买了当前课程
        if (!StringUtil.isNullOrEmpty(token)&&!"undefined".equals(token)) {
//            String userId = JwtUtils.tokenToId(token);
            boolean buyCourse = orderClient.isBuyCourse(courseId, token);
            return ResultData.ok().data("courseWebVo", courseWebVo)
                    .data("chapterVideoList", chapterVideoList)
                    .data("buyCourse", buyCourse)
                    .data("videoSourceId", videoSourceId);
        }
        //token值为空表示用户为登录，那么课程一定未购买
        return ResultData.ok().data("courseWebVo", courseWebVo)
                .data("chapterVideoList", chapterVideoList)
                .data("buyCourse", false)
                .data("videoSourceId", videoSourceId);
    }
}
